/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package demo.hello;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.rocketmq.client.consumer.DefaultLitePullConsumer;
import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
import org.apache.rocketmq.client.consumer.PullResult;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.message.MessageQueue;

/**
 * 拉模式的消费 需要客户端自己拉取 通过poll函数
 * @author zhangyuxi
 * @date 2022年1月28日 下午12:33:57
 */
public class PullConsumerTest {
    public static void main(String[] args) throws MQClientException {
        DefaultLitePullConsumer consumer = new DefaultLitePullConsumer("demo1");
        consumer.setNamesrvAddr("127.0.0.1:9876");
        consumer.subscribe("TopicTest2", "*");
//        Collection<MessageQueue> fetchMessageQueues = consumer.fetchMessageQueues("TopicTest1");
//        MessageQueue queue = fetchMessageQueues.iterator().next();
//        String brokerName = queue.getBrokerName();
//        System.out.println("broker name:"+brokerName);
        consumer.start();
        while(true){
            System.out.println("拉一下:"+new Date());
            // 默认阻塞5秒
            List<MessageExt> poll = consumer.poll();
            if(poll!=null){
                for(MessageExt msg:poll){
                    System.out.println(msg);
                    System.out.println(new String(msg.getBody()));
                }
            }
        }
//        System.out.println("退出循环");
        // 退出循环 但consumer不自动停止
//        consumer.shutdown();
    }
}
